package com.zhx.real.mybatisplus.business;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zhx.real.mybatisplus.constant.SqlConstant;
import com.zhx.real.mybatisplus.util.AddPojoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author zhanghaixuan
 */
public class BaseController<M extends IService<T>,T extends BaseEntity, ID extends Serializable> {

    @Autowired
    protected M baseService;

    public M getModelService() {
        return this.baseService;
    }

    /**
     * 获取所有的数据
     * @return 返回前端json数据
     */
    @GetMapping("/getDetail")
    public T getDetail(@RequestParam ID id){
        T detail= baseService.getById(id);
        return detail;
    }

    /**
     * 获取所有的数据
     * @return 返回前端json数据
     */
    @GetMapping("/getList")
    public List<T> getList(T pojo){
        List<T> list = baseService.list(Wrappers.query(pojo));
        return list;
    }

    /**
     * 分页查询
     * @param pojo  分页的实体类
     * @return 返回前端json数据
     */
    @GetMapping("/getPageList")
    public IPage<T> getPageList(Page page, T pojo){
        IPage<T> records = baseService.page(page, Wrappers.query(pojo));
        return records;
    }


    /**
     * 增加一条数据
     * @param pojo 任意一个类型的实体类
     * @return 返回前端json数据
     */
    @PostMapping("/addData")
    public T addData(@RequestBody T pojo){
        pojo.setDelFlag(SqlConstant.STATUS_NORMAL);
        pojo.setCreateTime(new Date());
        boolean flag = baseService.save(pojo);
        if(flag){
            return pojo;
        }else{
            throw new AddPojoException("添加失败");
        }
    }


    /**
     * 根据id修改一条数，数据中必须有主键id字段
     * @param pojo 任意实体类
     * @return 返回前端json数据
     */
    @PostMapping("/updateById")
    public T updateById(@RequestBody T pojo){
        pojo.setUpdateTime(new Date());
        boolean flag = baseService.updateById(pojo);
        if(flag){
            return pojo;
        }else{
            throw new AddPojoException("修改失败");
        }
    }


    /**
     * 根据id删除一条数据
     * @param id 传入的id值
     * @return 返回前端json数据
     */
    @PostMapping("/deleteById")
    public boolean delete(@RequestParam("id") ID id){
        boolean flag = baseService.removeById(id);

        return flag;
    }

    /**
     * 启用一条记录
     * @param id 记录id
     * @return 返回前端json数据
     */
    @PostMapping("/enable")
    public T enable(@RequestParam("id") ID id){
        T byId = baseService.getById(id);
        byId.setDelFlag(SqlConstant.STATUS_NORMAL);
        byId.setUpdateTime(new Date());
        boolean flag = baseService.updateById(byId);
        if(flag){
            return byId;
        }else{
            throw new AddPojoException("启用失败");
        }
    }

    /**
     * 禁用一条记录
     * @param id 记录id
     * @return 返回前端json数据
     */
    @PostMapping("/disable")
    public T disable(@RequestParam("id") ID id){
        T byId = baseService.getById(id);
        byId.setDelFlag(SqlConstant.STATUS_DEL);
        byId.setUpdateTime(new Date());
        boolean flag = baseService.updateById(byId);
        if(flag){
            return byId;
        }else{
            throw new AddPojoException("启用失败");
        }
    }
}
